package action;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import service.IVipService;
import util.Result;
import util.Result;
import vo.VipInfo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Controller
@Action(value = "vipAction")
public class VipAction extends BaseAction implements ModelDriven<VipInfo> {
	@Autowired
	protected IVipService vipService;
	public VipInfo vipinfo = new VipInfo();
	private int type;
	private String content;
	private int rows = 1;// 每页显示的记录数
	private int page = 20;// 当前第几页
	private int id;

	public String browseVip() throws Exception {
		List vip = vipService.getVip();
		Map request = (Map) ActionContext.getContext().get("request");
		request.put("vip", vip);
		return SUCCESS;

	}

	/**
	 * 添加操作
	 */
	public void getAllVip() {
		int total = vipService.getTotalNumVip();
		List<VipInfo> list = vipService.findAllCustomer(page, rows);
		try {
			writeJson(total, list);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void addVip() throws Exception {
		Result result = new Result();
		try {
			vipService.addVip(vipinfo);
			result.setSuccess(true);
			result.setMsg("添加客户成功");
		} catch (Exception e) {
			e.printStackTrace();
			result.setMsg("添加客户失败");
		}
		writeJson(result);
	}

	public void deleteVip() throws Exception {
		Result result = new Result();
		try {
			VipInfo vipinfo1 = vipService.findVipById(id);
			System.out.println(vipinfo1.getVipName());
			vipService.deleteVip(vipinfo1);
			result.setSuccess(true);
			result.setMsg("删除客户成功");
		} catch (Exception e) {
			e.printStackTrace();
			result.setMsg("删除客户失败");
		}
		writeJson(result);

		this.browseVip();
	}

	public String searchVip() throws Exception {
		System.out.println(type);
		StringBuffer hql = new StringBuffer("from VipInfo v");
		if (content != null && content.length() != 0 && type == 0) {
			hql.append(" where v.vipId like '%" + content + "%'");
		} else if (content != null && content.length() != 0 && type == 1) {
			hql.append(" where v.vipName like '%" + content + "%'");
		}
		System.out.println(hql);
		List vip = vipService.searchVip(hql.toString());
		Map request = (Map) ActionContext.getContext().get("request");
		request.put("vip", vip);
		return SUCCESS;

	}

	public String findVipById() throws Exception {

		vipinfo = vipService.findVipById(vipinfo.getVipId());
		Map request = (Map) ActionContext.getContext().get("request");
		request.put("vipinfo", vipinfo);
		return SUCCESS;
	}

	public void updateVip() throws Exception {
		Result result = new Result();
		try {
			vipService.updateVip(vipinfo);
			result.setSuccess(true);
			result.setMsg("修改客户成功");
			result.setSuccess(true);
		} catch (Exception e) {
			result.setMsg("添加客户失败");
		}
		writeJson(result);

	}

	public IVipService getVipService() {
		return vipService;
	}

	public void setVipService(IVipService vipService) {
		this.vipService = vipService;
	}

	public VipInfo getVipinfo() {
		return vipinfo;
	}

	public void setVipinfo(VipInfo vipinfo) {
		this.vipinfo = vipinfo;
	}

	public int getVipId() {
		return type;
	}

	public void setVipId(int vipId) {
		this.type = vipId;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	@Override
	public VipInfo getModel() {
		// TODO Auto-generated method stub
		return vipinfo;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

}
